<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;
use Doctrine\Common\Cache\FilesystemCache;
use Symfony\Component\HttpFoundation\Request;

class BaseCache
{
    private $cache ;
    public function __construct()
    {
        $this->cache = new FilesystemCache("../var/tmp/cache");
    }

    public function setItem($key,$value,$expireTime = "")
    {
        if ($expireTime) {
            return $this->cache->save($key,$value,$expireTime);
        }else{
            return $this->cache->save($key,$value);
        }

    }

    public function getItem($key)
    {
        return $this->cache->fetch($key);
    }

    public function getUserAccessToken(Request $request)
    {
        $access_token = $request->headers->get("access-token");
        if (!$access_token || $access_token == 'undefined') {
            $access_token = sha1(uniqid());
        }
        return $access_token;
    }

    //user token related
    public function createUserToken($access_token,$userInfo)
    {
        $this->setUserToken($access_token,$userInfo);
        return $access_token;
    }
    public function setUserToken($token,$userInfo)
    {
        return $this->cache->save($token,$userInfo,7*24*3600);
    }
}
